package com.gitee.wsl.net.server.upload

import com.gitee.wsl.io.File
import com.gitee.wsl.net.exceptions.Exceptions
import com.gitee.wsl.net.server.getStringParam
import com.gitee.wsl.net.server.role.admin.checkRoleAuth
import com.gitee.wsl.net.server.role.admin.checkRoleFull
import com.gitee.wsl.net.toExtension
import com.gitee.wsl.struct.generator.randomUUID
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.PartData
import io.ktor.http.content.forEachPart
import io.ktor.server.application.call
import io.ktor.server.request.receiveMultipart
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import kotlinx.io.files.Path

fun Route.uploads(uploadsService: UploadsService) {

    route("/uploads") {
        get("/{name}") {
            // check role
            call.checkRoleFull()
            // get request
            val name = call.getStringParam()
            /*val path = when {
                name.contains(".") -> "uploads/$name"
                name.contains("Outlined") -> "material-icons/svg/${name.toFileNameIcon()}/outline.svg"
                name.contains("Rounded") -> "material-icons/svg/${name.toFileNameIcon()}/round.svg"
                name.contains("TwoTone") -> "material-icons/svg/${name.toFileNameIcon()}/twotone.svg"
                name.contains("Sharp") -> "material-icons/svg/${name.toFileNameIcon()}/sharp.svg"
                else -> "material-icons/${name.toFileNameIcon()}/baseline.svg"
            }
            // act
            val file = File(Path(path))
            if (!file.exists()) throw Exceptions.NotFound()
            // response
            call.respondFile(file)*/
        }
        post {
            // check role
            call.checkRoleAuth()
            // get request
            val uploads = mutableListOf<UploadEntity>()
            // act
            val multipart = call.receiveMultipart()

            multipart.forEachPart { part ->
                if (part is PartData.FileItem) {

                    val name = "${randomUUID()}.${part.contentType?.toExtension()}"
                    val file = File("uploads/$name")

                    /*part.streamProvider().use { its ->
                        file.outputStream().buffered().use {
                            its.copyTo(it)
                        }
                    }

                    uploads.add(
                        uploadsService.transaction {
                            insert(
                                fileName = name,
                                contentType = part.contentType ?: throw Exceptions.BadRequest(),
                                originalFileName = part.originalFileName
                                    ?: throw Exceptions.BadRequest()
                            )
                        }
                    )*/
                }
                part.dispose()
            }
            // response
            call.respond(uploads)
        }
        delete("/{name}") {
            // check role
            call.checkRoleAuth()
            // get request
            val name = call.getStringParam()
            // delete db row
            /*uploadsService.transaction {
                deleteByFileName(name)
            }
            // delete file
            val file = File("uploads/$name")
            if (file.exists()) {
                file.delete()
                call.respond(HttpStatusCode.OK)
            } else throw Exceptions.NotFound()*/
        }
    }
}